import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Three {
    public static void main(String[] args) throws IOException {
        BufferedReader reader=new BufferedReader(new InputStreamReader(System.in));
        int n=Integer.parseInt(reader.readLine());
        String[] str=reader.readLine().split(" ");
        String s1=str[0],s2=str[1];
        int prev1=-1,prev2=-1,ret=0x3f3f3f3f;
        for (int i = 0; i < n; i++) {
            String s=reader.readLine();
            if(s.equals(s1)){//去前面照最近的s2
                if(prev2!=-1){
                    ret=Math.min(ret,i-prev2);
                }
                prev1=i;
            }
            if(s.equals(s2)){//去前面照最近的s1
                if(prev1!=-1){
                    ret=Math.min(ret,i-prev1);
                }
                prev2=i;
            }
        }
        System.out.println(ret==0x3f3f3f3f?-1:ret);
    }
}
